<?php
namespace frontend\models;

use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\web\IdentityInterface;


class IntLog extends ActiveRecord
{
    const STATUS_DELETED = 0;
    const STATUS_ACTIVE = 10;
    const STATUS = 1;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%int_log}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    public static function addData($data){
        $model = new static();
        if(!empty($data['member_id']))
            $model->member_id = $data['member_id'];
        if(!empty($data['weal_id']))
            $model->weal_id = $data['weal_id'];
        if(!empty($data['task_id']))
            $model->task_id = $data['task_id'];
        if(!empty($data['int']))
            $model->int = $data['int'];
        if(!empty($data['type']))
            $model->type = $data['type'];
        return $model->save();
    }

    /**
     * @inheritdoc
     */
    public static function selectFieldData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select([$field])->from('int_log');
        $query->where(['deleted_at' => null]);
        $query->orderBy([
            'created_at' => SORT_DESC,
        ]);
        if(!empty($data['member_id'])){
            $query->andWhere(['member_id' => $data['member_id']]);
        }
        if(!empty($data['type'])){
            $query->andWhere(['type' => $data['type']]);
        }

        $command = $query->createCommand();
        $result = $command->queryAll();
        foreach ($result as $k => $v){
            if(!empty($v['created_at'])){
                $result[$k]['created_at'] = date('Y-m-d H:i:s',$v['created_at']);
            }
        }
        return $result;
    }

    /**
     * @inheritdoc
     */
    public static function selectSumData($data = [],$field = '*')
    {
        $query = new \yii\db\Query();
        $query->select([$field])->from('int_log');
        $query->where(['deleted_at' => null]);
        $query->orderBy([
            'created_at' => SORT_DESC,
        ]);
        if(!empty($data['member_id'])){
            $query->andWhere(['member_id' => $data['member_id']]);
        }
        if(!empty($data['type'])){
            $query->andWhere(['type' => $data['type']]);
        }

        $command = $query->createCommand();
        $result = $command->queryAll();
        return $result;
    }



}
